/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * vwLogin.java
 *
 * Created on Apr 29, 2011, 3:30:32 PM
 */
package tcnplanner;

import java.awt.Container;
import javax.swing.JOptionPane;

/**
 *
 * @author Koen
 */
public class vwLogin extends javax.swing.JPanel {

    /** Creates new form vwLogin */
    
    public vwLogin() {
        initComponents();
        //this.jPanel1.imaet(ResourceHandler.getNxpLogo());
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gpLogin = new tcnplanner.GradientPanel();
        btLogin = new javax.swing.JButton();
        tfUsername = new javax.swing.JTextField();
        lbLogin = new javax.swing.JLabel();
        lbPassword = new javax.swing.JLabel();
        btRegistreer = new javax.swing.JButton();
        tfPassword = new javax.swing.JPasswordField();

        gpLogin.setBackground(new java.awt.Color(153, 153, 153));
        gpLogin.setForeground(new java.awt.Color(255, 255, 255));
        gpLogin.setDirection(2);

        btLogin.setText("Login");
        btLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLoginActionPerformed(evt);
            }
        });

        tfUsername.setText("Planner");

        lbLogin.setText("Username");

        lbPassword.setText("Password");

        btRegistreer.setText("Register");
        btRegistreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegistreerActionPerformed(evt);
            }
        });

        tfPassword.setText("test");

        javax.swing.GroupLayout gpLoginLayout = new javax.swing.GroupLayout(gpLogin);
        gpLogin.setLayout(gpLoginLayout);
        gpLoginLayout.setHorizontalGroup(
            gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gpLoginLayout.createSequentialGroup()
                .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, gpLoginLayout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbPassword)
                            .addComponent(lbLogin))
                        .addGap(18, 18, 18)
                        .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfPassword, 0, 0, Short.MAX_VALUE)
                            .addComponent(tfUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                        .addGap(29, 29, 29))
                    .addGroup(gpLoginLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btRegistreer, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btLogin)))
                .addContainerGap(130, Short.MAX_VALUE))
        );
        gpLoginLayout.setVerticalGroup(
            gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gpLoginLayout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbLogin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPassword)
                    .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(gpLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btLogin)
                    .addComponent(btRegistreer))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gpLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gpLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void setScreen(java.awt.Component s) {
        java.awt.Component mainpl = null;
        if (this.getParent().getName().equals("plMainPanel")) {
            mainpl = this.getParent();
        } else {
            java.awt.Component p = this.getParent();
            while (p == null && p.getParent()!=null) {
                p = p.getParent();
                if (p.getName().equals("plMainPanel"))
                {
                    mainpl = p;                    
                    Container tst1 = mainpl.getParent();
                    Container tst2 = tst1.getParent();
                }
            }
        }
        if (mainpl != null) {
            javax.swing.JPanel p = (javax.swing.JPanel) mainpl;
            p.removeAll();
            //SCREEN TYPE INFO
            java.awt.Component screen = s;
            //END OF SCREEN TYPE INFO
            javax.swing.GroupLayout plMainPanelLayout = new javax.swing.GroupLayout(p);
            p.setLayout(plMainPanelLayout);
            plMainPanelLayout.setHorizontalGroup(
                    plMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(screen, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE));
            plMainPanelLayout.setVerticalGroup(
                    plMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(screen, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE));
        }
    }
    private void btLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoginActionPerformed
        // TODO add your handling code here:
        String name = this.tfUsername.getText();
        String pass = this.tfPassword.getText();
        Account account = Main.controller.login(name, pass);
        if(account != null){
            setScreen(new vwBeginScherm(account.getActortype()));
        }else{
            JOptionPane.showMessageDialog(this, "paswoord of naam onjuist");
        }
       
    
    }//GEN-LAST:event_btLoginActionPerformed

    private void btRegistreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegistreerActionPerformed
        // TODO add your handling code here:
        setScreen(new vwRegister());
    }//GEN-LAST:event_btRegistreerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLogin;
    private javax.swing.JButton btRegistreer;
    private tcnplanner.GradientPanel gpLogin;
    private javax.swing.JLabel lbLogin;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration//GEN-END:variables
}
